package org.millioncall.yueyoga.admin.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.yueyoga.admin.dao.AuthorityDAO;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.RoleDAO;
import org.millioncall.yueyoga.admin.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class RoleDAOImpl extends BaseDao implements RoleDAO {
	public static final String NAME = "name";
	public static final String ENABLED = "enabled";

	@Autowired
	private AuthorityDAO authorityDAO;

	@Override
	public void save(Role role) {
		Map<String, Object> roles = new HashMap<String, Object>();
		roles.put("id", role.getId());
		roles.put("name", role.getName());
		roles.put("description", role.getDescription());
		roles.put("enabled", role.getEnabled());
		this.getPagableSqlSessionTemplate().insert("insert", roles);
	}

	@Override
	public void delete(Role role) {
		this.getPagableSqlSessionTemplate().delete("delete", role.getId());
	}

	@Override
	public Role findById(int id) {
		Role role = this.getPagableSqlSessionTemplate().selectOne("findById",
				id);
		return role;
	}

	@Override
	public List<Role> findByProperty(String propertyName, Object value) {
		List<Role> roles = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", propertyName);
		map.put("value", value);
		roles = this.getPagableSqlSessionTemplate().selectList(
				"findByProperty", map);
		return roles;
	}

	@Override
	public List<Role> findByName(String name) {
		List<Role> roles = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "name");
		map.put("value", name);
		roles = this.getPagableSqlSessionTemplate().selectList(
				"findByProperty", map);
		return roles;
	}

	@Override
	public List<Role> findByEnabled(Boolean enabled) {
		List<Role> roles = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "enabled");
		map.put("value", enabled);
		roles = this.getPagableSqlSessionTemplate().selectList(
				"findByProperty", map);
		return roles;
	}

	@Override
	public List<Role> findAll() {
		List<Role> roles = this.getPagableSqlSessionTemplate().selectList(
				"findAll");
		return roles;
	}

	@Override
	public List<Role> findAllByNames(List<String> names, boolean needEnabled) {
		List<Role> roles = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("names", names);
		map.put("enabled", needEnabled);
		roles = this.getPagableSqlSessionTemplate().selectList(
				"findAllRolesByNames", map);
		return roles;
	}

	@Override
	public List<Role> findRolesByGroupId(int gpId) {
		List<Role> roles = this.getPagableSqlSessionTemplate().selectList(
				"findRolesByGroupId", gpId);
		return roles;
	}

	@Override
	public List<Role> findRoleAll() {
		return this.getPagableSqlSessionTemplate().selectList("findRoleAll");
	}

	@Override
	public PaginatedList<Role> findRolesInPage(int start, int limit,
			Map<String, Object> map) {
		PreparedQuery<Role> pq = this.getPagableSqlSessionTemplate().findAll(
				"findRolesInPage", map);
		PaginatedList<Role> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl;
		
	}

	@Override
	public Role findRoleById(Integer id) {
		return this.getPagableSqlSessionTemplate()
				.selectOne("findRoleById", id);
	}

	@Override
	public int updateRole(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update("updateRole", map);
	}

	@Override
	public int saveRole(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert("saveRole", map);
	}

	@Override
	public void deleteRoleByIds(int[] id) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < id.length; i++) {
			map.put("id", id[i]);
			this.getPagableSqlSessionTemplate().delete("deleteRoleByIds", map);
		}
	}

	@Override
	public void deleteRoleGroupById(int[] id) {
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < id.length; i++) {
			map.put("id", id[i]);
			this.getPagableSqlSessionTemplate().delete("deleteRoleGroupById",
					map);
		}
	}

	@Override
	public List<Role> findRolesByGroupId(String gpId) {
		List<Role> roles = this.getPagableSqlSessionTemplate().selectList(
				"findRolesByGroupId", gpId);
		return roles;
	}

	@Override
	public int deleteRoleAuthorities(Integer roleId) {
		return this.getPagableSqlSessionTemplate().delete(
				"deleteRoleAuthorities", roleId);
	}

	@Override
	public int saveRoleAuthority(Map<String, String> map) {
		return this.getPagableSqlSessionTemplate().insert("saveRoleAuthority",
				map);
	}

	@Override
	public Role findRoleByName(String name) {
		return this.getPagableSqlSessionTemplate().selectOne("findRoleByName",name);
	}
}